package com.iplant.aps.serviceimpl.thread;

import com.iplant.aps.service.po.OutResult;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.sfc.SFCAttemptRun;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.APSServiceImpl;
import com.iplant.aps.serviceimpl.RPTServiceImpl;
import com.iplant.aps.serviceimpl.dao.BaseDAO;
import com.iplant.aps.serviceimpl.dao.andon.AndonDAO;
import com.iplant.aps.serviceimpl.dao.sfc.SFCAttemptRunDAO;
import com.iplant.aps.serviceimpl.utils.aps.APSConstans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class APSThread implements DisposableBean {
    private static final Logger logger = LoggerFactory.getLogger(APSThread.class);

    private final BMSEmployee AdminUser;
    boolean mIsStart;
    private int Ticks = 0;

    @PostConstruct
    public void init() {
        Run();
    }

    public APSThread() {
        this.AdminUser = BaseDAO.SysAdmin;
        this.mIsStart = false;

    }

    public APSThread(BMSEmployee wLoginUser) {
        this.mIsStart = false;
        this.AdminUser = wLoginUser;
    }

    private void Run() {

        try {
            if (this.mIsStart)
                return;
            this.mIsStart = true;

            new Thread(() -> {
                // 10秒后启动线程，防止其他服务未启动，导致访问超时
                try {
                    Thread.sleep(10000L);
                } catch (InterruptedException e) {
                    logger.error(StringUtils.Format("QMS start error:{0}", e.toString()));
                }
                logger.info("APSThread Started!!");

                while (this.mIsStart) {
                    try {
                        Thread.sleep(1000L);

                        if (this.Ticks % 10 == 0) {
                            // 安灯数据
                            AndonDAO.getInstance().Andon_UpdateBusiness(this.AdminUser);
                            // 触发日计划
                            APSServiceImpl.getInstance().APS_TriggerDayPlans(AdminUser);
                            // 流程引擎消息
                            this.SFCBiz();
                            // 次日重新制定日计划
//							APSServiceImpl.getInstance().APS_ReMakingDayPlan(AdminUser);
                        }
                        if (this.Ticks % 20 == 0) {
                            // 自动生成竣工确认单
//							APSServiceImpl.getInstance().APS_AutoGenerateConfirmForm(AdminUser);

                            // 出厂申请消息提示
                            APSServiceImpl.getInstance().APS_AutoSendMessageToApplyOutPlant(this.AdminUser);
                            // 安灯配置信息
                            AndonDAO.getInstance().Andon_ResetConfiguration(AdminUser);
                            // 自动同步台车BOM
//                            APSServiceImpl.getInstance().APS_HandleBOMOnTime();
                        }

                        if (this.Ticks % 60 == 0) {
                            // 自动完成工位任务
//							APSServiceImpl.getInstance().APS_AutoFinishTaskPart(AdminUser);
                            // 班次切换，新增日、周报表数据源
                            RPTServiceImpl.getInstance().RPT_SetProductShifts(AdminUser);
                        }

                        this.Ticks++;

                        if (Ticks > 10000)
                            this.Ticks = 1;

                    } catch (Exception ex) {
                        logger.error(ex.toString());
                    }
                }
            }).start();
        } catch (Exception ex) {
            logger.error(StringUtils.Format("QMS start failed error:{0}", ex.toString()));
        }
    }

    private void SFCBiz() {
        try {
            OutResult<Integer> wErrorCode = new OutResult<Integer>(0);
            SFCAttemptRun wSFCAttemptRun = null;
            for (int i = 0; i < APSConstans.SFCAttemptRunTaskResource.size(); i++) {
                wSFCAttemptRun = APSConstans.SFCAttemptRunTaskResource.get();
                boolean wIsTrue = SFCAttemptRunDAO.getInstance().AutoJudge(this.AdminUser, wSFCAttemptRun, wErrorCode);
                if (!wIsTrue)
                    APSConstans.SFCAttemptRunTaskResource.add(wSFCAttemptRun);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
    }

    public void destroy() {
        this.mIsStart = false;
    }
}